<?php
    class Produits extends FrontEnd_Controller {

        function Produits() {
            parent::FrontEnd_Controller();
            $this->load->helper('cookie');
            $this->load->helper('date');
            $this->load->model('produit_m');
            $this->load->library('form_validation');
            $this->_data['error'] ='';
            $this->_data['listprix'] = $this->produit_m->get_all_prix();
            $this->_data['listcat'] = $this->produit_m->get_all_categories();
          $this->_data['solde_produit']=$this->produit_m->get_soldes_produits(6,0);
          $this->_data['coeur_produit']=$this->produit_m->get_coeurs_produits(6,0);
        }

        function index(){
            $this->list_produits();


        }
        function list_produits($id=0){
            $catid = (int) $id;
            $this->_data['sidebar'] ='modules/sidebar';
            $this->_data['titre_page'] ='Zoo-market';

            $per_page = $this->uri->segment(4);
            $offset = $this->uri->segment(5);
            if ($per_page == ""
            )$per_page = 16;
            if ($offset == "")
                $offset = 0;
            $config['base_url'] = base_url() . 'index.php/produits/list_produits/' . $catid . '/' . $per_page . '/';
            $config['per_page'] = '16';
            $config['uri_segment'] = 5;
            $config['full_tag_open'] = "<div class='contain_page'>";
            $config['full_tag_close'] = '</div>';
            $config['cur_tag_open'] = '<span id="current_page"> [';
            $config['cur_tag_close'] = '] </span>';
            $config['next_link'] = '&gt;&gt;';
            $config['prev_link'] = '&lt;&lt;';
           
           
            if ($catid == 0) {
                $this->_data['is_child']=0;
                $listcatchild['array']=$this->produit_m->get_all_categories();
                $catimg['img'] = "";
                $config['total_rows'] = $this->db->count_all('zoo_produit');
                $listproduct = $this->produit_m->get_product($config['per_page'], $offset);
                $cat_link = "Tous les produits";
            } else {
                 $listcatchild = $this->produit_m->get_all_id_categories_child($catid);
                $catimg = $this->produit_m->get_img_categories($catid);
                 $this->_data['is_child']=$catimg['parent'];
                $config['total_rows'] = $this->produit_m->count_product_categories($listcatchild['id']);
                $listproduct = $this->produit_m->get_product_categories($listcatchild['id'], $config['per_page'], $offset);
                $cat_link = "<a href='" . site_url('produits') . "' >Tous les produits</a>";
                $cat_link.=' > ' . $catimg['nomfr'];
            }

            $this->pagination->initialize($config);
            $this->_data['cat_link'] = $cat_link;
            $this->_data['pagination'] = $this->pagination->create_links();
            $this->_data['listcatchild'] = $listcatchild['array'];
            $this->_data['listproduct'] = $listproduct;
  
            $this->display_view('produits');
        }
        function detail_produit($id){
            $id = (int) $id;
            $this->_data['sidebar'] ='modules/sidebar';
            $this->_data['titre_page'] ='Zoo-market';
            $produit=$this->produit_m->get_detail_product($id);
           
            $this->_data['produit'] = $produit;
            $this->display_view('detail_produit');
        }
        function recherche_mot(){
            $mot = mysql_real_escape_string($this->input->post('mot'));
            $prix=$this->input->post('prix');
            $cat=$this->input->post('cat');
            if($mot=="")$mot="_";
            if($prix=="")$prix="_";
            if($cat=="") $cat="_";
            redirect('produits/recherche/'.$mot.'/'.$prix.'/'.$cat);
        }
        function recherche($mot,$prix,$cat){
            $this->_data['sidebar'] ='modules/sidebar';
            $this->_data['titre_page'] ='Zoo-market';
            $this->_data['recherche_mot'] =$mot;
            $this->_data['recherche_prix'] =$prix;
            $this->_data['recherche_cat'] =$cat;

            $per_page = $this->uri->segment(6);
            $offset = $this->uri->segment(7);
            if ($per_page == ""
            )$per_page = 16;
            if ($offset == "")
                $offset = 0;
            $config['base_url'] = base_url() . 'index.php/produits/recherche/'.$mot.'/'.$prix.'/'.$cat.'/' . $per_page . '/';
            $config['per_page'] = '16';
            $config['uri_segment'] = 7;
            $config['full_tag_open'] = "<div class='contain_page'>";
            $config['full_tag_close'] = '</div>';
            $config['cur_tag_open'] = '<span id="current_page"> [';
            $config['cur_tag_close'] = '] </span>';
            $config['next_link'] = '&gt;&gt;';
            $config['prev_link'] = '&lt;&lt;';
            $config['total_rows'] = $this->produit_m->count_product_recherche($mot,$prix,$cat);
            $this->pagination->initialize($config);
            $this->_data['pagination'] = $this->pagination->create_links();




          
            $this->_data['listproduct'] = $this->produit_m->get_product_recherche($mot,$prix,$cat,$config['per_page'], $offset);
          
            $this->display_view('recherche');
        }
        function ajout_panier($id) {
            $id = (int) $id;
            $produit=$this->produit_m->get_detail_product($id);
            $data = array(
            'id' => $produit['id'],
            'qty' => 1,
            'price' => ($produit['prix']-$produit['promo']),
            'name' => $produit['nomfr'],
            'img' => $produit['img'],
            'poids' => $produit['poids']
            );
            $list_panier = $this->cart->contents();
            $existe=false;
            foreach ($list_panier as $aRow):
                if($aRow['id']==$id)  {
                    if($aRow['qty']<=($produit['quantite']-1)){
                    $data1 = array(
                    'rowid' => $aRow['rowid'],
                    'qty'   => $aRow['qty']+1
                    );

                    $this->cart->update($data1);
                    echo $this->cart->total();
                    }
                    $existe=true;
                    }
                endforeach;
            if(!$existe){
                if($produit['quantite']>=1){
                $this->cart->insert($data);
                echo $this->cart->total();
                }
            }

        }
       
         function up_panier($id) {
            $id = (int) $id;
            $produit=$this->produit_m->get_detail_product($id);
            $list_panier = $this->cart->contents();
            foreach ($list_panier as $aRow):
                if($aRow['id']==$id)  {
                    if($aRow['qty']<=($produit['quantite']-1)){
                    $data1 = array(
                    'rowid' => $aRow['rowid'],
                    'qty'   => $aRow['qty']+1
                    );
                    $this->cart->update($data1);
  redirect('produits/panier');
                    }
                    }
                endforeach;
           redirect('produits/panier');
        }
        
        
         function down_panier($id) {
            $list_panier = $this->cart->contents();
            foreach ($list_panier as $aRow):
                if($aRow['id']==$id)  {
                  $data1 = array(
                    'rowid' => $aRow['rowid'],
                    'qty'   => $aRow['qty']-1
                    );
          $this->cart->update($data1);
  redirect('produits/panier');
                    }
              
                endforeach;
           redirect('produits/panier');
        }
        

        function panier(){
            $this->_data['sidebar'] ='modules/sidebar';
            $this->_data['titre_page'] ='Panier';
            $list_panier = $this->cart->contents();
            $this->_data['list_panier'] = $list_panier;
            $this->display_view('panier');
        }
        function commandes(){
             is_user();
            $this->_data['sidebar'] ='modules/sidebar';
            $this->_data['titre_page'] ='Commandes';
       

            $per_page = $this->uri->segment(3);
            $offset = $this->uri->segment(4);
            if ($per_page == ""
            )$per_page = 16;
            if ($offset == "")
                $offset = 0;
            $config['base_url'] = base_url() . 'index.php/produits/commandes/'. $per_page . '/';
            $config['per_page'] = '16';
            $config['uri_segment'] = 4;
            $config['full_tag_open'] = "<div class='contain_page'>";
            $config['full_tag_close'] = '</div>';
            $config['cur_tag_open'] = '<span id="current_page"> [';
            $config['cur_tag_close'] = '] </span>';
            $config['next_link'] = '&gt;&gt;';
            $config['prev_link'] = '&lt;&lt;';
            $config['total_rows'] = $this->produit_m->count_commandes($this->session->userdata('user_id'));
            $this->pagination->initialize($config);
            $this->_data['pagination'] = $this->pagination->create_links();
            

            $this->_data['commandes'] =$this->produit_m->get_all_commandes($this->session->userdata('user_id'),$config['per_page'],$offset);
            $this->display_view('commandes');
        }

         function echantillons(){
              is_user();
            $this->_data['sidebar'] ='modules/sidebar';
            $this->_data['titre_page'] ='Commandes';


            $per_page = $this->uri->segment(3);
            $offset = $this->uri->segment(4);
            if ($per_page == ""
            )$per_page = 16;
            if ($offset == "")
                $offset = 0;
            $config['base_url'] = base_url() . 'index.php/produits/echantillons/'. $per_page . '/';
            $config['per_page'] = '16';
            $config['uri_segment'] = 4;
            $config['full_tag_open'] = "<div class='contain_page'>";
            $config['full_tag_close'] = '</div>';
            $config['cur_tag_open'] = '<span id="current_page"> [';
            $config['cur_tag_close'] = '] </span>';
            $config['next_link'] = '&gt;&gt;';
            $config['prev_link'] = '&lt;&lt;';
            $config['total_rows'] = $this->produit_m->count_all_echantillons($this->session->userdata('user_id'));
            $this->pagination->initialize($config);
            $this->_data['pagination'] = $this->pagination->create_links();


            $this->_data['echantillons'] =$this->produit_m->get_all_echantillons($this->session->userdata('user_id'),$config['per_page'],$offset);
            $this->display_view('echantillons');
        }


        function acheter_etape1(){
             is_user();
            $this->_data['info_promo'] ="";
            $formVal = array(
                array(
                'field' => 'livraison',
                'label' => 'Livraison',
                'rules' => 'required|xxs_clean'
                ),
                array(
                'field' => 'adr',
                'label' => 'Adresse',
                'rules' => 'required|xxs_clean'
                ),
                array(
                'field' => 'postale',
                'label' => 'Code postale',
                'rules' => 'required|exact_length[5]|xxs_clean'
                ),
                array(
                'field' => 'ville',
                'label' => 'Ville',
                'rules' => 'required|xxs_clean'
                ),
                array(
                'field' => 'promo',
                'label' => 'Code promo',
                'rules' => 'xxs_clean'
                ),
               );
             $this->form_validation->set_rules($formVal);
              if ($this->form_validation->run()==TRUE) {
                  $promo=$this->input->post('promo');
                 if($promo==""||$this->produit_m->check_code_promo($promo)) {
                    $newdata = array(
                   'zoo_livraison'  => $this->input->post('livraison'),
                   'zoo_adr'     => $this->input->post('adr'),
                   'zoo_postale'     => $this->input->post('postale'),
                   'zoo_ville'     => $this->input->post('ville'),
                   'vip'     => $this->input->post('vip'),
                   'zoo_promo' => $promo
               );
                $this->session->set_userdata($newdata);
                redirect('produits/acheter_etape2');
                 } else{
                     $this->_data['info_promo'] ="Le code de promotion n'est pas valide !";
                 }
              }

            $this->_data['sidebar'] ='modules/sidebar';
            $this->_data['titre_page'] ='etape1';
            $this->_data['user']=$this->produit_m->get_user($this->session->userdata('user_id'));
            $list_panier = $this->cart->contents();
            $poids= $this->produit_m->get_poids($list_panier);
            $this->_data['poids'] = $poids;
           $this->_data['livraison'] = $this->produit_m->get_livraison($poids);


            $this->display_view('etap1');
        }
         function acheter_etape2(){
              is_user();
              $this->_data['sidebar'] ='modules/sidebar';
            $this->_data['titre_page'] ='Etape 2';
             $livraison=$this->session->userdata('zoo_livraison');
             $this->_data['id_livraison']= $livraison;
             $list_panier = $this->cart->contents();
            $poids= $this->produit_m->get_poids($list_panier);
            $tarif=$this->db->get_where('zoo_tarif',array('id'=>$livraison))->row()->tarif;
            $frais_livraison=$poids*$tarif;
            
              $this->_data['frais_livraison']=$frais_livraison;
              
             $this->_data['adresse_livraison']= $this->session->userdata('zoo_adr');
             $this->_data['postale_livraison']= $this->session->userdata('zoo_postale');
              $this->_data['ville_livraison']= $this->session->userdata('zoo_ville');
             $this->_data['code_promo']= $this->session->userdata('zoo_promo');
             if($this->_data['code_promo']!=""){
             $this->_data['pct_promo']=$this->produit_m->get_pct_promo($this->_data['code_promo']);
             }
             $vip="";
            if($this->session->userdata('vip')!=""){
                 $user=$this->produit_m->get_user($this->session->userdata('user_id'));
                 $vip=$user['vip'] * 2.5;
            }
            $this->_data['vip']=$vip;
             $this->_data['client']=$this->produit_m->get_client($this->session->userdata('user_id'));



$sous_total=$this->cart->total();
$total=$sous_total;
 if(isset($this->_data['pct_promo'])) {
  if($this->_data['pct_promo']['type']=='1') {
   $total=$total-($sous_total*$this->_data['pct_promo']['pct']/100);
 }else { $total=$total-$this->_data['pct_promo']['pct']; }}
$total=$total+$frais_livraison;
if($vip!="") $total=$total-$vip;
if($total<=0) $total=0;

$newdata = array('zoo_montant'  => $total);
$this->session->set_userdata($newdata);
              $this->display_view('etap2');
         }
        function supprimer_tous_panier_redirect() {
            $this->cart->destroy();
            redirect(site_url('produits/panier'));
        }

        function supprimer_produit_panier($rowid) {
            $data = array(
            'rowid' => $rowid,
            'qty' => 0
            );
            $this->cart->update($data);
            redirect(site_url('produits/panier'));
        }

        function echantillon($id_produit){

            $id_user=$this->session->userdata('user_id');
            if($id_user==NULL||$id_user==""){
                echo "0";
            }else{
                $query= $this->produit_m->check_echantillon($id_user,$id_produit);
                if($query->num_rows()>0){
                    echo "3";
                }else {
                    $query= $this->produit_m->add_echantillon($id_user,$id_produit);
                    if($query) echo "1";  else echo "2";
            }}

        }
    }
?>
